package com.lq.educationsystem.teacher.service.impl;

import com.lq.educationsystem.common.entiy.*;
import com.lq.educationsystem.common.util.*;
import com.lq.educationsystem.common.vo.*;
import com.lq.educationsystem.teacher.entiy.TeacherTb;
import com.lq.educationsystem.teacher.feign.*;
import com.lq.educationsystem.teacher.mapper.TeacherTbMapper;
import com.lq.educationsystem.teacher.service.TeacherBaseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TeacherBaseImpl implements TeacherBaseService {

    //注入feign
    @Autowired
    SubjectFeign subjectFeign;

    @Autowired
    TeacherTbMapper teacherTbMapper;
    @Autowired
    ClassesFeign classesFeign;
    //学生服务feign
    @Autowired
    StudentFeign studentFeign;
    //考试feign
    @Autowired
    ExaminationFeign examinationFeign;

    @Autowired
    ScoreFeign scoreFeign;

    /**
     *
     * @param teacherId
     * @return
     */
    @Override
    public Result<TeacherTb> queryById(Integer teacherId) {
        //通过id查询用户信息
        TeacherTb teacherInfo = teacherTbMapper.selectByPrimaryKey(Long.valueOf(teacherId));
        return ResultBuild.buildOk(new Result<TeacherTb>(),teacherInfo);
    }

    @Override
    public void insertTeacherInfo(TeacherTb teacherTb) {
        //插入信息
        int row = teacherTbMapper.insertSelective(teacherTb);
        System.out.println("影响行数:"+row);
    }

    @Override
    public void updateTeacherInfo(TeacherTb teacherTb) {
        //用户信息
        int row = teacherTbMapper.updateByPrimaryKeySelective(teacherTb);

        System.out.println("信息输出:"+row);
    }

    /**
     * 教师信息
     * @param teacherId
     */
    @Override
    public void deleteTeacherInfo(Integer teacherId) {
        int row = teacherTbMapper.deleteByPrimaryKey(Long.valueOf(teacherId));

        System.out.println("教师删除影响行数:"+row);
    }

    @Override
    public ResultPage<TeacherTb> queryList(TeacherPageVo teacherInfo) {
        //数据更新
        Integer page = teacherInfo.getPage();
        Integer pageSize = teacherInfo.getPageSize();
        int start = PageUtil.getStart(page, pageSize);

        TeacherTb teacherTb = new TeacherTb();
        BeanUtils.copyProperties(teacherInfo,teacherTb);
        //数据更新
        List<TeacherTb>  teacherTbs =teacherTbMapper.queryList(start,pageSize,teacherTb);

        int counts= 0;

        //页数查询
        counts = teacherTbMapper.queryCount(teacherTb);
        return ResultPageBuild.pageBuildOk(new ResultPage<TeacherTb>(),page,pageSize,counts,teacherTbs);
    }

    @Override
    public ResultPage<TeacherTb> queryBatchTeacherInfoByTeacherIdList(List<Integer> teacherIdListInfo) {
        //批量查询数据
        List<TeacherTb> teacherTbList = teacherTbMapper.queryBatchTeacherInfoByTeacherIdList(teacherIdListInfo);
        ResultPage<TeacherTb> teacherTbResultPage = new ResultPage<>();
        teacherTbResultPage.setData(teacherTbList);
        return teacherTbResultPage;
    }
    /**
     * 通过教师id查询所授班级学生的信息
     * @param classesPageInfo
     * @return
     */
    @Override
    public ResultPage<SubjectClassesStudentVo> querySubjectClassesStudentInfoByTeacherId(ClassesPageVo classesPageInfo) {
        //查询班级信息
        Integer classesId = classesPageInfo.getClassesId();
        ClassesTb classesTb = new ClassesTb();
        classesTb.setClassesId(classesId);
        Result<ClassesTb> classesTbResult = classesFeign.queryClassesById(classesTb);
        ClassesTb classesInfo = (ClassesTb) classesTbResult.getData();
        //查询班级学生 id 集合
        ClassesStudentPageVo classesStudentPageVo = new ClassesStudentPageVo();
        classesStudentPageVo.setPage(classesPageInfo.getPage());
        classesStudentPageVo.setPageSize(classesPageInfo.getPageSize());

        //设置班级id
        classesStudentPageVo.setClassesId(classesId);

        ResultPage<ClassesStudentTb> classesStudentTbResultPage = classesFeign.queryClassesStudentInfoListByClassesId(classesStudentPageVo);
        List<ClassesStudentTb> classesStudentInfoList = classesStudentTbResultPage.getData();

        //收集班级内学生的id
        List<Integer> collectStudentIdInfoList = classesStudentInfoList.stream().map((item) -> {
            return item.getStudentId();
        }).collect(Collectors.toList());

        ResultPage<StudentTb> studentTbResultPage = studentFeign.queryBatchStudentInfoByStudentIdList(collectStudentIdInfoList);
        List<StudentTb> studentInfoList = studentTbResultPage.getData();
        //然后拼接到一起返回给前端

        ArrayList<SubjectClassesStudentVo> subjectClassesStudentVos = new ArrayList<>();

        for (int i = 0; i < studentInfoList.size(); i++) {

            StudentTb studentTb = studentInfoList.get(i);
            //拼接数据
            SubjectClassesStudentVo subjectClassesStudentVo = new SubjectClassesStudentVo();

            //拼接班级信息
            subjectClassesStudentVo.setClassesId(classesInfo.getClassesId());
            subjectClassesStudentVo.setClassesName(classesInfo.getClassesName());
            //拼接学生信息
            subjectClassesStudentVo.setStudentId(studentTb.getStudentId());
            subjectClassesStudentVo.setStudentName(studentTb.getStudentName());
            subjectClassesStudentVo.setStudentAccount(studentTb.getStudentAccount());
            subjectClassesStudentVo.setStudentXueHao(studentTb.getStudentXueHao());

            subjectClassesStudentVos.add(subjectClassesStudentVo);
        }


        return ResultPageBuild.pageBuildOk(new ResultPage<SubjectClassesStudentVo>(),classesStudentPageVo.getPage(),classesStudentPageVo.getPageSize(),classesStudentTbResultPage.getCountTotal(),subjectClassesStudentVos);
    }
    /**
     * 通过教师id查询教师所授课程
     * @param teacherPageVo
     * @return
     */
    @Override
    public ResultPage<TeacherSubjectClassesVo> queryTeacherSubjectInfoByTeacherId(TeacherPageVo teacherPageVo) {
        //查出教师信息
        TeacherTb teacherTbInfo = teacherTbMapper.selectByPrimaryKey(Long.valueOf(teacherPageVo.getTeacherId()));
        System.out.println("teacherTbInfo = " + teacherTbInfo);
        //第一步根据教师id查出 教师所授课程
        ResultPage<TeacherSubjectTb> teacherSubjectTbResultPage = subjectFeign.queryTeacherSubjectInfoByTeacherId(teacherTbInfo.getTeacherId());
        //第二步获取课程id
        List<TeacherSubjectTb> teacherSubjectTbResultPageData = teacherSubjectTbResultPage.getData();
        List<Integer> collectSubjectIdInfoList = teacherSubjectTbResultPageData.stream().map((item) -> {
            return item.getSubjectId();
        }).collect(Collectors.toList());
        //第三步根据课程id查出课程信息
        ResultPage<SubjectTb> subjectTbResultPage = subjectFeign.queryBatchSubjectInfoBySubjectIdList(collectSubjectIdInfoList);
        List<SubjectTb> subjectInfoList = subjectTbResultPage.getData();


        //获取teacher_subject_id 集合
        List<Integer> collectTeacherSubjectIdInfoList = teacherSubjectTbResultPageData.stream().map((item) -> {
            return item.getTeacherSubjectId();
        }).collect(Collectors.toList());
        //第四步根据teacher_subject_id 查出教师所授班级的id

        ResultPage<ClassesSubjectTb> classesSubjectTbResultPage = classesFeign.queryClassesSubjectInfoByTeacherSubjectId(collectTeacherSubjectIdInfoList);

        List<ClassesSubjectTb> data = classesSubjectTbResultPage.getData();

        //获取班级id
        List<Integer> collectClassesIdInfoList = data.stream().map((item) -> {
            return item.getClassesId();
        }).collect(Collectors.toList());
        //收集班级信息
        //第五步查出班级信息
        //这里界定教师所教课程的班级只有1班 相当与一一对应
        ResultPage<ClassesTb> classesTbResultPage = classesFeign.queryClassesInfoByClassesId(collectClassesIdInfoList);
        List<ClassesTb> classesInfoList = classesTbResultPage.getData();
        //拼装 教师数据 课程数据 班级

        //这里拼接课程数据就好了
        //班级就先不拼接了
        ArrayList<TeacherSubjectClassesVo> teacherSubjectClassesVos = new ArrayList<>();
        //拼接数据
        for (int i = 0; i < subjectInfoList.size(); i++) {
            TeacherSubjectClassesVo teacherSubjectClassesVo = new TeacherSubjectClassesVo();

            teacherSubjectClassesVo.setTeacherId(teacherTbInfo.getTeacherId());
            teacherSubjectClassesVo.setTeacherNo(teacherTbInfo.getTeacherNo());
            teacherSubjectClassesVo.setTeacherAccount(teacherTbInfo.getTeacherAccount());
            teacherSubjectClassesVo.setTeacherName(teacherTbInfo.getTeacherName());

            ClassesTb classesTb = classesInfoList.get(i);
            //拼接班级信息
            teacherSubjectClassesVo.setClassesName(classesTb.getClassesName());
            teacherSubjectClassesVo.setClassesId(classesTb.getClassesId());

            SubjectTb subjectTb = subjectInfoList.get(i);
            //拼接课程信息
            teacherSubjectClassesVo.setSubjectId(subjectTb.getSubjectId());
            teacherSubjectClassesVo.setSubjectName(subjectTb.getSubjectName());

            teacherSubjectClassesVos.add(teacherSubjectClassesVo);

        }
        ResultPage<TeacherSubjectClassesVo> teacherSubjectClassesVoResultPage = new ResultPage<>();
        //
        teacherSubjectClassesVoResultPage.setData(teacherSubjectClassesVos);
        teacherSubjectClassesVoResultPage.setCode(200);
        teacherSubjectClassesVoResultPage.setPageSize(subjectInfoList.size());
        teacherSubjectClassesVoResultPage.setCountTotal(subjectInfoList.size());


        return teacherSubjectClassesVoResultPage;
    }

    /**
     * 教师发布考试信息
     * @param teacherSubjectExaminationInfo
     * @return
     */
    @Override
    public Result teacherReleaseSubjectExaminationInfo(TeacherSubjectExaminationVo teacherSubjectExaminationInfo) {

        /**
         * 考试信息发布
         */
        Result<ExaminationTb> examinationTbResult = examinationFeign.addExaminationInfoByExaminationInfoAndSubjectId(teacherSubjectExaminationInfo);
        ExaminationTb examinationInfo = (ExaminationTb) examinationTbResult.getData();
        //获取考试id
        Integer examinationId = examinationInfo.getExaminationId();
        //查出班级内所有的学生
        ClassesStudentPageVo classesStudentPageVo = new ClassesStudentPageVo();
        classesStudentPageVo.setClassesId(teacherSubjectExaminationInfo.getClassesId());
        //设置起始页数
        classesStudentPageVo.setPage(1);
        //设置取值  大一点 省的取值取不玩
        classesStudentPageVo.setPageSize(9999);

        ResultPage<ClassesStudentTb> classesStudentTbResultPage = classesFeign.queryClassesStudentInfoListByClassesId(classesStudentPageVo);

        //学生信息集合
        List<ClassesStudentTb> studentTbResultPageData = classesStudentTbResultPage.getData();

        List<Integer> collectStudentIdInfoList = studentTbResultPageData.stream().map((item) -> {
            return item.getStudentId();
        }).collect(Collectors.toList());
        //根据考试id和学生id集合 批量添加 student_examination表的信息
        Result result = examinationFeign.addBatchStudentExaminationInfoByExaminationIdAndStudentId(collectStudentIdInfoList, examinationId);
        //批量添加数据
        //分数表也需要修改
        scoreFeign.addSubjectStudentScoreByStudentIdAndSubjectId(collectStudentIdInfoList, teacherSubjectExaminationInfo.getSubjectId());


        return result;
    }

    /**
     * 学生考试分数查询 通过班级id和 课程id   班级id查询班级内所有的的学生  学生id集合和课程id查询当前科目
     * @param classesStudentSubjectScorePageVo
     * @return
     */
    @Override
    public ResultPage<SubjectClassesStudentScoreVo> querySubjectClassesStudentScoreByTeacherId(ClassesStudentSubjectScorePageVo classesStudentSubjectScorePageVo) {
        //信息获取
        Integer pageSize = classesStudentSubjectScorePageVo.getPageSize();
        Integer page = classesStudentSubjectScorePageVo.getPage();
        Integer subjectId = classesStudentSubjectScorePageVo.getSubjectId();

        SubjectTb subjectTb = new SubjectTb();
        subjectTb.setSubjectId(subjectId);
        //获取班级信息
        Result<SubjectTb> subjectTbResult = subjectFeign.queryById(subjectTb);
        SubjectTb subjectInfo = (SubjectTb) subjectTbResult.getData();


        Integer classesId = classesStudentSubjectScorePageVo.getClassesId();
        //第一步根据班级学生id查班级内学生的id
        ClassesStudentPageVo classesStudentPageVo = new ClassesStudentPageVo();
        classesStudentPageVo.setClassesId(classesId);
        classesStudentPageVo.setPageSize(pageSize);
        classesStudentPageVo.setPage(page);
        //获取学生信息
        ResultPage<ClassesStudentTb> classesStudentTbResultPage = classesFeign.queryClassesStudentInfoListByClassesId(classesStudentPageVo);
        List<ClassesStudentTb> classesStudentInfoList = classesStudentTbResultPage.getData();
        //classesStudentTbResultPage 获取总数

        //收集学生id
        List<Integer> collectStudentIdInfoList = classesStudentInfoList.stream().map((item) -> {
            return item.getStudentId();
        }).collect(Collectors.toList());
        //第二步根据学生id和课程id 查询班级内学生的分数
        ResultPage<ScoreTb> scoreTbResultPage = scoreFeign.querySubjectStudentScoreByStudentId(collectStudentIdInfoList, subjectId);
        List<ScoreTb> scoreInfoList = scoreTbResultPage.getData();

        System.out.println("scoreInfoList = " + scoreInfoList);


        //第三步查出班级id信息
        ClassesTb classesTb = new ClassesTb();
        classesTb.setClassesId(classesId);
        Result<ClassesTb> classesTbResult = classesFeign.queryClassesById(classesTb);
        ClassesTb classesInfo = (ClassesTb) classesTbResult.getData();
        System.out.println("classesInfo = " + classesInfo);
        //第四步 根据学生id 查出学生的信息
        ResultPage<StudentTb> studentTbResultPage = studentFeign.queryBatchStudentInfoByStudentIdList(collectStudentIdInfoList);
        List<StudentTb> studentInfoList = studentTbResultPage.getData();
        System.out.println("studentInfoList = " + studentInfoList);
        //第五步拼接数据到一起
        ArrayList<SubjectClassesStudentScoreVo> subjectClassesStudentScoreVos = new ArrayList<>();
        //根据学生信息集合拼接数据
        for (int i = 0; i < studentInfoList.size(); i++) {
            SubjectClassesStudentScoreVo subjectClassesStudentScoreVo = new SubjectClassesStudentScoreVo();

            //拼接班级信息
            subjectClassesStudentScoreVo.setClassesName(classesInfo.getClassesName());
            subjectClassesStudentScoreVo.setClassesId(classesInfo.getClassesId());
            //拼接学生信息
            StudentTb studentTb = studentInfoList.get(i);
            subjectClassesStudentScoreVo.setStudentAccount(studentTb.getStudentAccount());
            subjectClassesStudentScoreVo.setStudentId(studentTb.getStudentId());
            subjectClassesStudentScoreVo.setStudentName(studentTb.getStudentName());
            subjectClassesStudentScoreVo.setStudentXueHao(studentTb.getStudentXueHao());
            //拼接班级信息
            subjectClassesStudentScoreVo.setSubjectName(subjectInfo.getSubjectName());
            subjectClassesStudentScoreVo.setSubjectId(subjectInfo.getSubjectId());
            //分数拼接
            ScoreTb scoreTb = scoreInfoList.get(i);
            subjectClassesStudentScoreVo.setScore(scoreTb.getScore());
            subjectClassesStudentScoreVo.setScoreId(scoreTb.getScoreId());


            subjectClassesStudentScoreVos.add(subjectClassesStudentScoreVo);

        }

        //返回拼接的数据
        return ResultPageBuild.pageBuildOk(new ResultPage<SubjectClassesStudentScoreVo>(),page,pageSize,classesStudentTbResultPage.getCountTotal(),subjectClassesStudentScoreVos);
    }

    @Override
    public Result teacherUpdateStudentScoreBySubjectIdAndStudentId(SubjectStudentScoreVo subjectStudentScoreVo) {
        //调用feign
        Result result = scoreFeign.updateStudentScoreByStudentIdAndSubjectId(subjectStudentScoreVo);

        return result;
    }
}
